﻿
Imports std
Imports command = centrosphere.job.storage.command

Public Class storageJobRcEmitter
    Inherits rc_pac_emitter

    Public Sub New(ByVal host As String, ByVal port As Int32,
                   Optional ByVal holdtimeMs As Int64 = tcpTalk4.defaultHoldTimeMs,
                   Optional ByVal livetimeMs As Int64 = tcpTalk4.defaultLiveTimeMs)
        MyBase.New(host, port, holdtimeMs, livetimeMs)
    End Sub

    Private Overloads Function runjob(ByVal setjob As void(Of storageJob),
                                      ByVal setresult As void(Of storageJob),
                                      ByRef ec As eventComb) As Boolean
        assert(Not setjob Is Nothing)
        assert(Not setresult Is Nothing)
        Dim job As storageJob = Nothing
        job = New storageJob()
        setjob(job)
        Dim ec2 As eventComb = Nothing
        ec = New eventComb(Function() As Boolean
                               Return eva(ec2, runjob(job)) AndAlso
                                      waitfor(ec2) AndAlso
                                      goto_next()
                           End Function,
                           Function() As Boolean
                               If ec2.end_result() Then
                                   setresult(job)
                                   Return goto_end()
                               Else
                                   Return False
                               End If
                           End Function)
        Return True
    End Function

    Private Shared Function j(ByVal cmd As command,
                              Optional ByVal key As String = Nothing,
                              Optional ByVal buff As bytes = Nothing) As void(Of storageJob)
        Return Sub(ByRef sj As storageJob)
                   sj.command = cmd
                   sj.key = key
                   sj.buff = buff
               End Sub
    End Function

    Private Shared Function r(ByVal result As pointer(Of Boolean)) As void(Of storageJob)
        Return Sub(ByRef sj As storageJob)
                   eva(result, sj.result)
               End Sub
    End Function

    Private Shared Function r(ByVal result As pointer(Of Int64)) As void(Of storageJob)
        Return Sub(ByRef sj As storageJob)
                   eva(result, sj.i64)
               End Sub
    End Function

    Public Function write(ByVal key As String,
                          ByVal buff As bytes,
                          ByVal result As pointer(Of Boolean),
                          ByRef ec As eventComb) As Boolean
        Return runjob(j(command.write, key, buff),
                      r(result),
                      ec)
    End Function

    Public Function uniqueWrite(ByVal key As String,
                                ByVal buff As bytes,
                                ByVal result As pointer(Of Boolean),
                                ByRef ec As eventComb) As Boolean
        Return runjob(j(command.uniqueWrite, key, buff),
                      r(result),
                      ec)
    End Function

    Public Function delete(ByVal key As String,
                           ByVal result As pointer(Of Boolean),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.delete, key),
                      r(result),
                      ec)
    End Function

    Public Function modify(ByVal key As String,
                           ByVal buff As bytes,
                           ByVal result As pointer(Of Boolean),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.modify, key, buff),
                      r(result),
                      ec)
    End Function

    Public Function read(ByVal key As String,
                         ByVal buff As pointer(Of bytes),
                         ByVal ts As pointer(Of Int64),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.read, key),
                      Sub(ByRef sj As storageJob)
                          eva(buff, sj.buff)
                          eva(ts, sj.i64)
                      End Sub,
                      ec)
    End Function

    Public Function seek(ByVal key As String,
                         ByVal result As pointer(Of Boolean),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.seek, key),
                      r(result),
                      ec)
    End Function

    Public Function capacity(ByVal result As pointer(Of Int64),
                             ByRef ec As eventComb) As Boolean
        Return runjob(j(command.capacity),
                      r(result),
                      ec)
    End Function

    Public Function valuesize(ByVal result As pointer(Of Int64),
                              ByRef ec As eventComb) As Boolean
        Return runjob(j(command.valuesize),
                      r(result),
                      ec)
    End Function

    Public Function list(ByVal result As pointer(Of vector(Of String)),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.list),
                      Sub(ByRef sj As storageJob)
                          eva(result, sj.vs)
                      End Sub,
                      ec)
    End Function

    Public Function status(ByVal all As pointer(Of Int64),
                           ByVal unhealth As pointer(Of Int64),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.status),
                      Sub(ByRef sj As storageJob)
                          eva(all, sj.i64)
                          eva(unhealth, sj.i64_2)
                      End Sub,
                      ec)
    End Function

    Public Function open(ByVal result As pointer(Of Boolean),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.open),
                      r(result),
                      ec)
    End Function

    Public Function close(ByVal result As pointer(Of Boolean),
                          ByRef ec As eventComb) As Boolean
        Return runjob(j(command.close),
                      r(result),
                      ec)
    End Function

    Public Function full(ByVal result As pointer(Of Boolean),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.full),
                      r(result),
                      ec)
    End Function

    Public Function health(ByVal result As pointer(Of Boolean),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.health),
                      r(result),
                      ec)
    End Function

    Public Function retire(ByVal result As pointer(Of Boolean),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.retire),
                      r(result),
                      ec)
    End Function

    Public Function empty(ByVal result As pointer(Of Boolean),
                          ByRef ec As eventComb) As Boolean
        Return runjob(j(command.empty),
                      r(result),
                      ec)
    End Function

    Public Function sizeof(ByVal key As String,
                           ByVal result As pointer(Of Int64),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.sizeof, key),
                      r(result),
                      ec)
    End Function

    Public Function keycount(ByVal result As pointer(Of Int64),
                             ByRef ec As eventComb) As Boolean
        Return runjob(j(command.keycount),
                      r(result),
                      ec)
    End Function

    Public Function sync(ByVal host As String,
                         ByVal port As Int64,
                         ByVal result As pointer(Of Boolean),
                         ByRef ec As eventComb) As Boolean
        Return runjob(Sub(ByRef sj As storageJob)
                          sj.command = command.sync
                          sj.key = host
                          sj.i64 = port
                      End Sub,
                      r(result),
                      ec)
    End Function

    Public Function version(ByVal result As pointer(Of String),
                            ByRef ec As eventComb) As Boolean
        Return runjob(j(command.version),
                      Sub(ByRef sj As storageJob)
                          eva(result, sj.key)
                      End Sub,
                      ec)
    End Function

    Public Function lockwrite(ByVal result As pointer(Of Boolean),
                              ByRef ec As eventComb) As Boolean
        Return runjob(j(command.lockwrite),
                      r(result),
                      ec)
    End Function

    Public Function unlockwrite(ByVal result As pointer(Of Boolean),
                                ByRef ec As eventComb) As Boolean
        Return runjob(j(command.unlockwrite),
                      r(result),
                      ec)
    End Function

    Public Function lockread(ByVal result As pointer(Of Boolean),
                             ByRef ec As eventComb) As Boolean
        Return runjob(j(command.lockread),
                      r(result),
                      ec)
    End Function

    Public Function unlockread(ByVal result As pointer(Of Boolean),
                               ByRef ec As eventComb) As Boolean
        Return runjob(j(command.unlockread),
                      r(result),
                      ec)
    End Function

    Public Function lock(ByVal result As pointer(Of Boolean),
                         ByRef ec As eventComb) As Boolean
        Return runjob(j(command.lock),
                      r(result),
                      ec)
    End Function

    Public Function unlock(ByVal result As pointer(Of Boolean),
                           ByRef ec As eventComb) As Boolean
        Return runjob(j(command.unlock),
                      r(result),
                      ec)
    End Function
End Class
